//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Fdm2DimSolver.h"
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSolverDesc.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpComposite.h>
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::CFdm2DimSolver (Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ schemeDesc, Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpComposite^ op) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmSolverDesc^ _CsolverDesc;
    CFdmSchemeDesc^ _CschemeDesc;
    CFdmLinearOpComposite^ _Cop;
    try
    {
#ifdef HANDLE
        _phFdm2DimSolver = NULL;
#endif
        _CsolverDesc = safe_cast<CFdmSolverDesc^> (solverDesc);
        _CsolverDesc->Lock();
        QuantLib::FdmSolverDesc& _solverDesc = static_cast<QuantLib::FdmSolverDesc&> (_CsolverDesc->GetReference ()); 
        _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc);
        _CschemeDesc->Lock();
        QuantLib::FdmSchemeDesc& _schemeDesc = static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()); 
        _Cop = safe_cast<CFdmLinearOpComposite^> (op);
        _Cop->Lock();
        boost::shared_ptr<QuantLib::FdmLinearOpComposite>& _op = static_cast<boost::shared_ptr<QuantLib::FdmLinearOpComposite>&> (_Cop->GetShared ()); 
        _ppFdm2DimSolver = new boost::shared_ptr<QuantLib::Fdm2DimSolver> (new QuantLib::Fdm2DimSolver ( _solverDesc,  _schemeDesc,  _op ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CsolverDesc != nullptr) _CsolverDesc->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
        if (_Cop != nullptr) _Cop->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::CFdm2DimSolver (boost::shared_ptr<QuantLib::Fdm2DimSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdm2DimSolver = NULL;
#endif
	_ppFdm2DimSolver = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::CFdm2DimSolver (QuantLib::Fdm2DimSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdm2DimSolver = NULL;
#endif
	_ppFdm2DimSolver = new boost::shared_ptr<QuantLib::Fdm2DimSolver> (&childNative);
    
    _Fdm2DimSolverOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::CFdm2DimSolver (CFdm2DimSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdm2DimSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdm2DimSolver = new boost::shared_ptr<QuantLib::Fdm2DimSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::CFdm2DimSolver (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdm2DimSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdm2DimSolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::CFdm2DimSolver (QuantLib::Handle<QuantLib::Fdm2DimSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdm2DimSolver = &childNative;
	_ppFdm2DimSolver = &static_cast<boost::shared_ptr<QuantLib::Fdm2DimSolver>>(childNative.currentLink());
    
    _Fdm2DimSolverOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::CFdm2DimSolver (QuantLib::Handle<QuantLib::Fdm2DimSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdm2DimSolver = &childNative;
	_ppFdm2DimSolver = &static_cast<boost::shared_ptr<QuantLib::Fdm2DimSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::CFdm2DimSolver (QuantLib::Fdm2DimSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdm2DimSolver = NULL;
#endif
	_ppFdm2DimSolver = new boost::shared_ptr<QuantLib::Fdm2DimSolver> (new QuantLib::Fdm2DimSolver (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::~CFdm2DimSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdm2DimSolver != NULL)
    {
	    delete _ppFdm2DimSolver;
        _ppFdm2DimSolver = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::!CFdm2DimSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdm2DimSolver != NULL)
    {
	    delete _ppFdm2DimSolver;
    }
}
QuantLib::Fdm2DimSolver& Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::GetReference ()
{
    if (_ppFdm2DimSolver == NULL) throw REFNEW NativeNullException ();
	return **_ppFdm2DimSolver;
}
boost::shared_ptr<QuantLib::Fdm2DimSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::GetShared ()
{
    if (_ppFdm2DimSolver == NULL) throw REFNEW NativeNullException ();
	return *_ppFdm2DimSolver;
}
QuantLib::Fdm2DimSolver* Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::GetPointer ()
{
    if (_ppFdm2DimSolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdm2DimSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Fdm2DimSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::GetHandle ()
{
	if (_phFdm2DimSolver == NULL)
	{
		_phFdm2DimSolver = new Handle<QuantLib::Fdm2DimSolver> (*_ppFdm2DimSolver);
	}
	return *_phFdm2DimSolver;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::HasNative () 
{
	return (_ppFdm2DimSolver != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::DerivativeX (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2DimSolver)->derivativeX ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::DerivativeXX (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2DimSolver)->derivativeXX ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::DerivativeXY (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2DimSolver)->derivativeXY ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::DerivativeY (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2DimSolver)->derivativeY ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::DerivativeYY (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2DimSolver)->derivativeYY ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::InterpolateAt (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2DimSolver)->interpolateAt ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver::ThetaAt (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdm2DimSolver)->thetaAt ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Solvers::IFdm2DimSolver^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdm2DimSolver_Factory::Create (Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ schemeDesc, Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpComposite^ op)
{
    return REFNEW CFdm2DimSolver ( solverDesc,  schemeDesc,  op);
}
